package br.ufrj.ppgi.greco.model.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.caelum.vraptor.ioc.Component;
import br.ufrj.ppgi.greco.model.entity.EntityWebHook;


@Component
public class DAOWebHook extends DAOGeneric<EntityWebHook>
{
	private EntityManager em;
	
	public DAOWebHook(EntityManager em)
	{
		super(em);
		this.em = em;
	}
	

	public EntityWebHook getHookByURL(String url)
	{
		String querySearch = "select ew from " + EntityWebHook.class.getSimpleName() + " ew where ew.url = :url";
		Query q = this.em.createQuery(querySearch);
		q.setParameter("url", url);
		return (EntityWebHook) q.getSingleResult();
	}
	
	public String getAllHooksJson()
	{
		String result = "[";
		List<EntityWebHook> listHooks=  this.getAll();
		for(EntityWebHook hook : listHooks)
		{
			result+= "'"+hook.getUrl()+"',";
		}	
		result += "];";
		return result;
	}
}